﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using SD.Database;
using SD.Database.Classes;
using smallDeimos.Game.Controls;

namespace smallDeimos.Game
{
    public partial class Science : System.Web.UI.Page
    {
        string lbl = null;
        int UserId;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
            StringBuilder sb = new StringBuilder("You are researching ");
            switch (UserFunctions.PlayerCurrentScience(UserId))
            {
                case 1: sb.Append("scaners.");
                    //lbl = "Biology";
                    break;
                case 2: sb.Append("economy.");
                    //lbl = "Economy";
                    break;
                case 3: sb.Append("energy.");
                    //lbl = "Energy";
                    break;
                case 4: sb.Append("math.");
                    //lbl = "Math";
                    break;
                case 5: sb.Append("physics.");
                    //lbl = "Physics";
                    break;
                case 6: sb.Append("social.");
                    //lbl = "Social";
                    break;
            }
            //Response.Write(lbl);
            //if (lbl != null)
            //{
            //    foreach (DataListItem item in DataList1.Items)
            //    {
            //        Label l = (Label)item.FindControl(lbl);
            //        l.BackColor = Color.Red;
            //    }
            //}
            try
            {
                Label1.Text = sb.ToString();
                Label2.Text = "Your science output without modifiers is: " + UserFunctions.GetPlayerSciencePerH(UserId).ToString(); // +" and with modifiers: " + UserDefinedFunctions.GetPlayerSciencePerHWithBonus(UserId).ToString();
                Label3.Text = "Your culture output without modifiers is: " + UserFunctions.GetPlayerCulturePerH(UserId).ToString();// +" and with modifiers: " + UserDefinedFunctions.GetPlayerCulturePerHWithBonus(UserId).ToString();
            }
            catch (Exception ex)
            {
                Session["Error"] = "Unknow error occured\n" + ex.ToString();
                Response.Redirect("~/Error.aspx");
            }
        }
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            switch (UserFunctions.PlayerCurrentScience(UserId))
            {
                case 1:
                    lbl = "BiologyLabel";
                    break;
                case 2:
                    lbl = "EconomyLabel";
                    break;
                case 3:
                    lbl = "EnergyLabel";
                    break;
                case 4:
                    lbl = "MathLabel";
                    break;
                case 5:
                    lbl = "PhysicsLabel";
                    break;
                case 6:
                    lbl = "SocialLabel";
                    break;
            }
            if (e.Item.ItemType == ListItemType.Item && lbl != null)
            {
                //Response.Write(lbl + "<br>");
                Label l = (Label)e.Item.FindControl(lbl);
                if (l != null)
                    l.BackColor = Color.Red;
            }
        }
        protected Color ColorForActiveScience(object sender)
        {
            try
            {
                if (sender.ToString().Equals(lbl) == true)
                    return Color.BlueViolet;
                return Color.Transparent;
            }
            catch (Exception)
            {
                return Color.Transparent;
            }
        }
        protected string RemainingTimeForScience(object factor)
        {

            double currentScience;
            if (double.TryParse(factor.ToString(), out currentScience) == true)
            {
                int levelPoints = Equations.ScienceCost((int)currentScience);
                double currentValue = (currentScience - (int)currentScience) * levelPoints * 1.0;
                int output = UserFunctions.GetPlayerSciencePerHWithBonus(UserId);
                if (output == 0)
                    return "NaN";
                TimeSpan ts = TimeSpan.FromHours((levelPoints - currentValue) / (output * 1.0));
                return string.Format("{0:00}h:{1:00}m:{2:00}s", (int)ts.TotalHours, ts.Minutes, ts.Seconds);
            }
            return string.Empty;
        }
        protected string RemainingTimeForCulture(object factor)
        {
            double cLv;
            if (double.TryParse(factor.ToString(), out cLv) == true)
            {
                int levelPoints = Equations.CultureCost((int)cLv);
                double currentValue = (cLv - (int)cLv) * levelPoints * 1.0;
                int output = UserFunctions.GetPlayerCulturePerHWithBonus(UserId);
                if (output == 0)
                    return "NaN";
                TimeSpan ts = TimeSpan.FromHours((levelPoints - currentValue) / (output * 1.0));
                return string.Format("{0:00}h:{1:00}m:{2:00}s", (int)ts.TotalHours, ts.Minutes, ts.Seconds);
            }
            return string.Empty;
        }
        protected void SelectScience(int i)
        {
            int UserId;
            if (Request.Cookies["sd_id"] != null)
            {
                HttpCookie cookie2 = Request.Cookies["sd_id"];
                UserId = int.Parse(cookie2.Value);
            }
            else
            {
                Session["Error"] = "Cookie sd_id doesn't exists";
                Response.Redirect("~/Error.aspx");
                return;
            }
            //Response.Write(i.ToString());
            StoredProcedures.SetScience(UserId, i);
            Page.Response.Redirect(Page.Request.Url.ToString());
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            SelectScience(2);
        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            SelectScience(3);
        }

        protected void Button5_Click(object sender, EventArgs e)
        {
            SelectScience(4);
        }

        protected void Button6_Click(object sender, EventArgs e)
        {
            SelectScience(5);
        }

        protected void Button7_Click(object sender, EventArgs e)
        {
            SelectScience(6);
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            SelectScience(1);
        }


    }
}
